package com.zadtek.sharkRider.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.OrderDetailActivity_;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.OrderDeliveryItemData;
import com.zadtek.sharkRider.views.OnChanged;

import java.util.List;

public class OrderDeliveryItemAdapter extends
		CommonAdapter<OrderDeliveryItemData> {
	private OnChanged listener;
	private String mainid;	//主订单id
	private String sendaddess;
	public OrderDeliveryItemAdapter(Context context,
			List<OrderDeliveryItemData> datas, int layoutId, String mainid,String sendaddess) {
		super(context, datas, layoutId);
		this.mainid = mainid;
		// TODO Auto-generated constructor stub
		this.sendaddess =sendaddess;
	}

	@Override
	public void convert(final ViewHolders holder, final OrderDeliveryItemData t) {
		ImageView newtask_order_img = holder.getView(R.id.delivery_order_img);
		TextView newtask_goods_name = holder.getView(R.id.delivery_goods_name);
		TextView newtask_goods_address = holder
				.getView(R.id.delivery_goods_address);
		LinearLayout getlay = holder.getView(R.id.getlay);

		final Button delivery_button = holder.getView(R.id.delivery_button);
		//2,配送中（确认送达），4.已送达，3.已取消，1未取货
		//状态改为 1：配送中，2：未取货，3：取消中，4：已送达，5：已取消
		if (t.getStatus().equals("1")) {
			delivery_button.setText("确认送达");
			delivery_button.setBackgroundResource(R.drawable.delivery_submit1);
		}else if (t.getStatus().equals("5") || t.getStatus().equals("3") || t.getStatus().equals("6")) {
			delivery_button.setText("已取消");
			delivery_button.setBackgroundResource(R.drawable.delivery_submit2);
		} else if (t.getStatus().equals("4")) {
			delivery_button.setText("已送达");
			delivery_button.setBackgroundResource(R.drawable.delivery_submit2);
		}

		newtask_goods_name.setText(t.getSite_name());
		newtask_goods_address.setText(t.getSite_addr());

		delivery_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (t.getStatus().equals("3") ||t.getStatus().equals("5") ||t.getStatus().equals("6") || t.getStatus().equals("4")) {
					return;
				}else if(t.getStatus().equals("1") ){
					SharedPreferences sp = mContext.getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
					String empid = sp.getString("uid", "");
					if (!empid.equals("") && empid !=null) {
						
						listener.getChoiceData(t.getOrder_id(), empid,holder.getPositon());
					}
				}
			}
		});
		
		getlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, OrderDetailActivity_.class);
				intent.putExtra("mainid", mainid);
				intent.putExtra("status", "ps");
				intent.putExtra("address", sendaddess);
				mContext.startActivity(intent);
			}
		});
	}

	public void setOnChanged(OnChanged listener) {
	this.listener = listener;
}
}
